#include "Ctrl.h"


#if AbstractFactory_ 

class Department
{
private:
    int m_id;
    string m_deptName;
public :
    int GetID()const{
        return m_id;
    }
    void SetID(const int _id){
        m_id = _id;
    }
    string GetDeptName()const{
        return m_deptName;
    }
    void SetDeptName(const string _name){
        m_deptName = _name;
    }
};


class IDepartment
{
public :
    virtual void Insert(Department* department) = 0;
    virtual Department* GetDepartment(int id) = 0;
};


class SqlserverDepartment : public IDepartment
{
public :
    void Insert(Department* department)override{
        cout << "Insert Department into SQL" << endl;
    }
    Department* GetDepartment(int id)override{
        cout << "Get Department from SQL" << endl;
        return nullptr;
    }
};

class AccessDepartment : public IDepartment
{
public :
    void Insert(Department* department)override{
        cout << "Insert Department into Access" << endl;
    }
    Department* GetDepartment(int id)override{
        cout << "Get Department from Access" << endl;
        return nullptr;
    }
};


class User
{
private :
    int m_id;
    string m_name;
public :
    int GetID()const{
        return m_id;
    }
    void SetID(const int _id){
        m_id = _id;
    }
    string GetName()const{
        return m_name;
    }
    void SetName(const string _name){
        m_name = _name;
    }
};




// IUser接口，用于客户端访问，解除与具体数据库访问的耦合
class IUser
{
public :
    virtual void Insert(User* user) = 0;
    virtual User* GetUser(int id) = 0;
};

// SqlserverUser类，用于访问SQL Server 的 User
class SqlserverUser : public IUser
{
public :
    void Insert(User* user)override{
        cout << "Insert User into SQL" << endl;
    }
    User* GetUser(int id)override{
        cout << "Get User from SQL" << endl;
        return nullptr;
    }
};

// AccessUser类，用于访问Access 的 User
class AccessUser : public IUser
{
public :
    void Insert(User* user)override{
        cout << "Insert User into Access" << endl;
    }
    User* GetUser(int id)override{
        cout << "Get User from Access" << endl;
        return nullptr;
    }
};



class IFactory
{
public :
    virtual IUser* CreateUser() = 0;
    virtual IDepartment* CreateDepartment() = 0;

};

// SqlServerFactory类，实现 IFactory 接口，实例化SalserverUser 和 SqlserverDepartment
class SqlServerFactory : public IFactory
{
public:
    IUser* CreateUser()override{
        return new SqlserverUser();
    }
    IDepartment* CreateDepartment()override{
        return new SqlserverDepartment();
    }
};

// AccessFactory类，实现 IFactory 接口，实例化AccessUser 和 AccessDepartment
class AccessFactory : public IFactory
{
public:
    IUser* CreateUser()override{
        return new AccessUser();
    }
    IDepartment* CreateDepartment()override{
        return new AccessDepartment();
    }
};


// 客户端代码
int main(){
    User* pUser = new User();
    Department* pDept = new Department();
    
    // 只需 确定实例化哪一个数据库访问对象给factory
    // IFactory* pFactory = new SqlServerFactory();
    IFactory* pFactory = new AccessFactory();

    IUser* pIUser = pFactory->CreateUser();

    pIUser->Insert(pUser);
    pIUser->GetUser(1);

    IDepartment* pIDepartment = pFactory->CreateDepartment();

    pIDepartment->Insert(pDept);
    pIDepartment->GetDepartment(1);

    // delete pUser;
    // delete pDept;
    // delete pFactory;
    // delete pIUser;
    // delete pIDepartment;
    // delete pFactory;
    // delete pUser;
    // delete pDept;
}




#endif